package awesome.cpu.structure;

public class MuxImm2Ex extends NonSequentialComponent {
	
	public final static int KEY_IMM_SRC = 1;
	private int mImmSrc;
	
	public final static int KEY_IMM14 = 2;
	private int mImm14;
	public final static int KEY_IMM24 = 3;
	private int mImm24;
	
	public final static int KEY_SELECTED = 4;
	//private int mSelected;

	private final static MuxImm2Ex mInstance = new MuxImm2Ex();
	
	private MuxImm2Ex() {}
	
	public static MuxImm2Ex getInstance() {
		return mInstance;
	}

	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_IMM_SRC:
			mImmSrc = data;
			return true;
		case KEY_IMM14:
			mImm14 = data;
			return true;
		case KEY_IMM24:
			mImm24 = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {

		BusMonitor.transferData(Control.getInstance(), Control.KEY_IMM_SRC,
				this, KEY_IMM_SRC);
		BusMonitor.transferData(InstructionRegister.getInstance(),
				InstructionRegister.KEY_INSTRUCTION_23_0,
				this, KEY_IMM24);
		BusMonitor.transferData(InstructionRegister.getInstance(),
				InstructionRegister.KEY_INSTRUCTION_13_0,
				this, KEY_IMM14);
		
		switch (keyInterface) {
		case KEY_SELECTED:
			return select();
		}
		return 0;
	}

	private int select() {
		switch (mImmSrc) {
		case 0:
			return mImm14;
		case 1:
			return mImm24;
		}
		return 0;
	}
}
